package com.powernode.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.shop.constant.ProdPropConstant;
import com.powernode.shop.domain.ProdProp;
import com.powernode.shop.domain.ProdPropValue;
import com.powernode.shop.mapper.ProdPropMapper;
import com.powernode.shop.mapper.ProdPropValueMapper;
import com.powernode.shop.service.ProdPropService;
import com.powernode.shop.service.ProdPropValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author ZHAOZHENYU
* @since 2022/10/12
*/
@Service
@CacheConfig(cacheNames = "com.powernode.shop.service.impl.ProdPropServiceImpl")
public class ProdPropServiceImpl extends ServiceImpl<ProdPropMapper, ProdProp> implements ProdPropService{

    @Autowired
    private ProdPropMapper prodPropMapper;

    @Autowired
    private ProdPropValueMapper prodPropValueMapper;

    @Autowired
    private ProdPropValueService prodPropValueService;

    @Override
    public Page<ProdProp> selectProdPropPage(Page<ProdProp> page, ProdProp prodProp) {

        Page<ProdProp> prodPropPage = prodPropMapper.selectPage(page, new LambdaQueryWrapper<ProdProp>()
                .like(StringUtils.hasText(prodProp.getPropName()), ProdProp::getPropName, prodProp.getPropName()));
        // 取出 分页数据规格集合 的地址，后续改了后反馈到堆里面
        List<ProdProp> prodPropList = prodPropPage.getRecords();
        if (CollectionUtils.isEmpty(prodPropList)) {
            return prodPropPage;
        }

        /*
            技巧点：用集合去查询value表的数据，不要一条一条的查
         */
        List<Long> propIdList = prodPropList.stream().map(ProdProp::getPropId).collect(Collectors.toList());

        List<ProdPropValue> prodPropValueList = prodPropValueMapper.selectList(new LambdaQueryWrapper<ProdPropValue>()
                .in(ProdPropValue::getPropId, propIdList));

        // 查出的值放入prodProp集合
        prodPropList.forEach(prodProp1 -> {
            List<ProdPropValue> prodPropValues = prodPropValueList.stream()
                    .filter(prodPropValue ->
                            prodPropValue.getPropId().equals(prodProp1.getPropId())
                     ).collect(Collectors.toList());
            prodProp1.setProdPropValues(prodPropValues);
        });

        // 直接返回 ， 因为page里面存的 是规格集合的地址，堆里面改了则page也改了
        return prodPropPage;
    }

    /**
     * 2、重写save
     * 删除缓存
     * @param prodProp
     * @return
     */
    @Override
    @Transactional
    @CacheEvict(key = ProdPropConstant.PROD_PROP_LIST)
    public boolean save(ProdProp prodProp) {

        prodProp.setShopId(1L);
        prodProp.setRule(1);
        int num = prodPropMapper.insert(prodProp);

        if (num ==0){
            // 手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        List<ProdPropValue> prodPropValues = prodProp.getProdPropValues();
        prodPropValues.forEach(prodPropValue -> {
            prodPropValue.setPropId(prodProp.getPropId());
        });
        boolean b = prodPropValueService.saveBatch(prodPropValues);
        if (!b){
            // 手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return num > 0 && b;
    }

    /**
     * 3、添加商品：查询所有规格
     * 放入缓存
     * @return
     */
    @Override
    @Cacheable(key = ProdPropConstant.PROD_PROP_LIST)
    public List<ProdProp> list() {
        return prodPropMapper.selectList(null);
    }
}
